﻿Imports Odyssee.WebControles
Imports OpenDelib.ServiceDeliberations

Public Class ListeRecueils
    Inherits OpenDelibPage


    Protected ReadOnly Property TypeDocumentRecueil As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.TypeDocument, TypeDeliberation.Recueil)
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Master.Titre = "Liste des recueils"

        If Not IsPostBack Then
            ' Chargement des recueils
            InitialiserRecueils()
        End If
    End Sub


    Private Sub btnActualiser_Click(sender As Object, e As EventArgs) Handles btnActualiser.Click
        InitialiserRecueils()
    End Sub

    Private Sub btnGenererRecueil_ValueReturned(sender As Object, e As JQPopupPanel.ValueReturnedEventArgs) Handles btnGenererRecueil.ValueReturned
        If IsNumeric(e.Value) AndAlso CInt(e.Value) > 0 Then InitialiserRecueils()
    End Sub

    Private Sub gvRecueils_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvRecueils.RowCommand
        Dim cleDocument As Integer = 0

        If e.CommandArgument Is Nothing OrElse Not IsNumeric(e.CommandArgument) Then Return

        cleDocument = CInt(e.CommandArgument)
        If cleDocument <= 0 Then Return

        Select Case e.CommandName.ToUpper
            Case "SUPPRIMER"
                Using svc As New ServiceDocuments.DocumentsClient
                    Dim params As New ServiceDocuments.ParametresSuppressionDocument
                    params.Authentication = New ServiceDocuments.AuthenticationParameters
                    params.Authentication.Token = Tools.GetInstance.getToken
                    params.Cle = cleDocument
                    params.SuppressionTotale = True

                    Try
                        Dim results As ServiceDocuments.OperationResult
                        results = svc.Suppression(params)
                        If results.OK Then
                            NotifySuccess("Suppression effectuée avec succès.")
                            InitialiserRecueils()
                        Else
                            NotifyErreur(results.ErrorMessage)
                        End If
                    Catch ex As Exception
                        NotifyErreur(ex.Message)
                    End Try
                End Using

        End Select
    End Sub



    Protected Sub InitialiserRecueils()
        Using svc As New ServiceDocuments.DocumentsClient
            Dim params As New ServiceDocuments.CriteresRechercheDocument
            params.Authentication = New ServiceDocuments.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.Type = Me.TypeDocumentRecueil

            Dim results As ServiceDocuments.ResultatRechercheDocument = Nothing
            Try
                results = svc.Recherche(params)
                If results Is Nothing OrElse Not results.OK Then
                    NotifyErreur("Impossible de charger la liste des recueils " & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage))
                Else
                    gvRecueils.DataSource = results.Documents
                    gvRecueils.DataBind()
                End If
            Catch ex As Exception
                NotifyErreur(ex.Message)
            End Try
        End Using
    End Sub

End Class